package dcc.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.Iterator;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

import com.l2fprod.common.swing.JLinkButton;
import com.l2fprod.common.swing.JTaskPaneGroup;

import dcc.app.Application;
import dcc.app.ManagerGroup;
import dcc.component.JLinkButtonX;
import dcc.component.JTaskPaneX;
import dcc.mbg.model.User;
import dcc.tools.Tools;

public class MainWindow extends JFrame {

	// private Application app;
	private ManagerGroup managerGroup;

	private User currentUser;
	private JPanel leftPanel;
	private JPanel rightPanel;
	private Tools tools;

	public ManagerGroup getManagerGroup() {
		return managerGroup;
	}

	public void addManager(String managerName) {
		if (this.managerGroup != null) {
			managerGroup.add(managerName);
		}
	}

	public MainWindow(Application app, User currentUser) {
		// this.app = app;
		this.currentUser = currentUser;
		managerGroup = new ManagerGroup();
		Tools.get().setUser(currentUser);
		Tools.get().addManagerGroup(managerGroup);
		Tools.get().addSearchWindowComponent(new SearchWindow());
		initialize();
	}

	private void initialize() {
		setExtendedState(Frame.MAXIMIZED_BOTH);
		this.setIconImage(new ImageIcon(MainWindow.class
				.getResource("/dcc/resources/images/ico.png")).getImage());
		setName("");
		setBounds(0, 0, 980, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(Tools.get().getValue("LABEL_MAIN_WINDOW_TITLE"));
		initWidget();
		setVisible(true);
	}

	private JTaskPaneX taskPane;

	private void initWidget() {
		JSplitPane splitPane = new JSplitPane();
		splitPane.setDoubleBuffered(true);
		getContentPane().add(splitPane, BorderLayout.CENTER);

		leftPanel = new JPanel();
		leftPanel.setMinimumSize(new Dimension(250, 10));
		splitPane.setLeftComponent(leftPanel);
		leftPanel.setLayout(new BorderLayout(0, 0));

		rightPanel = new JPanel();
		splitPane.setRightComponent(rightPanel);
		rightPanel.setLayout(new BorderLayout(0, 0));

		// add menu
		JScrollPane menuScrollPane = new JScrollPane();
		menuScrollPane.setDoubleBuffered(true);
		menuScrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		leftPanel.add(menuScrollPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 1000));
		menuScrollPane.setViewportView(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0 };
		gbl_panel.rowHeights = new int[] { 320, 0 };
		gbl_panel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		taskPane = new JTaskPaneX();
		Tools.get().addTaskPaneComponent(taskPane);

		buildCurrentUserInfoBlock(taskPane);

		tools = Tools.get();
		String[] menus = tools.getValue("MENUS").toString().split(",");
		for (String menu : menus) {
			if (tools.getUser().getRole()
					.equals(tools.getValue("ROLE").split(";")[0])) {
				if (menu.equals("INFORMATIONMANAGE")
						|| menu.equals("DATAEXPORT")) {
					;
				} else {
					createMenu(menu, null);
				}
			} else {
				if (menu.equals("OPTION")) {
					createMenu(menu, "OPTION_USERS");
				} else {
					createMenu(menu, null);
				}
			}
		}
		GridBagConstraints gbc_taskPane = new GridBagConstraints();
		gbc_taskPane.fill = GridBagConstraints.BOTH;
		gbc_taskPane.gridx = 0;
		gbc_taskPane.gridy = 0;
		panel.add(taskPane, gbc_taskPane);
	}

	private void createMenu(String menu, String disableItem) {
		String title = tools.getValue(menu);
		JTaskPaneGroup taskPaneGroup = new JTaskPaneGroup();
		taskPaneGroup.setDoubleBuffered(true);
		taskPaneGroup.setTitle(title);
		taskPane.add(taskPaneGroup);
		taskPaneGroup.getContentPane().setLayout(new GridLayout(0, 1, 0, 2));
		String menuItemKey = menu + "_";
		Set<Object> menuItems = tools.keySet();
		Iterator<Object> it = menuItems.iterator();
		while (it.hasNext()) {
			String item = it.next().toString();
			if (item.contains(menuItemKey)) {
				if (disableItem != null && !disableItem.equals("")
						&& disableItem.equals(item)) {
					continue;
				} else {
					String[] menuItemInfo = tools.getValue(item).toString()
							.split(";");
					JLinkButtonX lnkbtn = new JLinkButtonX(this,
							this.rightPanel, menuItemInfo[2]);
					this.addManager(menuItemInfo[2]);
					lnkbtn.setIcon(new ImageIcon(MainWindow.class
							.getResource("/dcc/resources/images/"
									+ menuItemInfo[0] + ".png")));
					lnkbtn.setText(menuItemInfo[1]);
					lnkbtn.setToolTipText(menuItemInfo[1]);
					taskPaneGroup.getContentPane().add(lnkbtn);
				}
			}
		}
	}

	private void buildCurrentUserInfoBlock(JTaskPaneX taskPane) {
		JTaskPaneGroup currentUserPaneGroup = new JTaskPaneGroup();
		currentUserPaneGroup.setDoubleBuffered(true);
		currentUserPaneGroup.setTitle(Tools.get()
				.getValue("LABEL_CURRENT_USER"));
		taskPane.add(currentUserPaneGroup);
		currentUserPaneGroup.getContentPane().setLayout(
				new GridLayout(0, 1, 0, 2));
		String userName = Tools.get().getValue("LABEL_USERNAME") + ": "
				+ this.currentUser.getUserName();
		buildUserInfoItem(currentUserPaneGroup, "talents", userName);
		String name = Tools.get().getValue("LABEL_NAME") + ": "
				+ this.currentUser.getName();
		buildUserInfoItem(currentUserPaneGroup, "talents", name);
		String role = Tools.get().getValue("LABEL_ROLE") + ": "
				+ this.currentUser.getRole();
		buildUserInfoItem(currentUserPaneGroup, "talents", role);
	}

	private void buildUserInfoItem(JTaskPaneGroup currentUserPaneGroup,
			String imageName, String title) {
		JLinkButton item = new JLinkButton();
		item.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
		item.setForeground(Color.BLUE);
		item.setIcon(new ImageIcon(MainWindow.class
				.getResource("/dcc/resources/images/" + imageName + ".png")));
		item.setText(title);
		item.setToolTipText(title);
		item.setBorder(new EmptyBorder(4, 4, 4, 4));
		currentUserPaneGroup.getContentPane().add(item);
	}
}
